//
//  LYSubjectSelectedViewController.m
//  artist
//
//  Created by laotang on 16/5/14.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYSubjectSelectedViewController.h"
#import <Masonry.h>
#import "LYSelectedClassesViewController.h"
#import "QNUploadManager.h"
#import "QNUploadOption.h"
#import "SVProgressHUD.h"
@interface LYSubjectSelectedViewController ()
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, assign) NSInteger uploadIndex; // 当前上传的图片下标
@property (nonatomic, strong) NSMutableString *imageLink;
@property (nonatomic, copy)  void(^uploadImageCompleteHandle)(NSString *);

@end

@implementation LYSubjectSelectedViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.editContentView setMy_Y: 44];
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, LYScreenWidth  -20, 44)];
    [self.view addSubview:self.textField];
    self.textField.placeholder = @"请输入标题";
    self.textField.textAlignment = NSTextAlignmentLeft;
    self.textField.borderStyle= UITextBorderStyleNone;
    
    UIView *lineView= [[UIView alloc] initWithFrame:CGRectMake(0, 44, LYScreenWidth, 0.5)];
    lineView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
    [self.view addSubview:lineView];
    WS(weakSelf)
    [self.editContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo( 44);
        make.height.mas_equalTo(170);
    }];
//    self.rightButton.hidden 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一步"  style:UIBarButtonItemStylePlain target:self  action:@selector(publicData)];
    self.title = @"课程信息";
}



//    s

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)publicData{
    
    if (!self.textField.text){
        [SVProgressHUD showErrorWithStatus:@"请输入标题"];
        return;
    }
    
    if (!self.textView.text){
        [SVProgressHUD showErrorWithStatus:@"请输入标题"];
        return;

    }
    
    LYUserInfoModel *userInfo = [VVYSaveDataManager getDataWithKey:KEY_USERMESSAGE];
    if (userInfo.role_id != 1 && self.choseBigImageLists.count == 0){
        [SVProgressHUD showErrorWithStatus:@"请选择图片"];

    }
    self.uploadModel.title = self.textField.text;
    self.uploadModel.desc = self.textView.text;
    WS(weakSelf)

    if (self.choseBigImageLists.count){
        [self setUploadImageCompleteHandle:^(NSString *imageLink) {
            if (imageLink.length > 1){
                NSString *linK  =  [imageLink substringToIndex:(self.imageLink.length - 1)];
                weakSelf.uploadModel.images = linK;
                LYSelectedClassesViewController *selectVC = [[LYSelectedClassesViewController alloc] init];
                selectVC.uploadModel = weakSelf.uploadModel;
                [weakSelf.navigationController pushViewController:selectVC animated:YES];
            }
        }];
    }else{
        LYSelectedClassesViewController *selectVC = [[LYSelectedClassesViewController alloc] init];
        selectVC.uploadModel = weakSelf.uploadModel;
        [weakSelf.navigationController pushViewController:selectVC animated:YES];
    }
    

    self.imageLink = [NSMutableString string];
    [self uploadImagesuccess:nil];
    
    
    

}


- (void)uploadImagesuccess:(void (^)(NSString * imageLink))success{
    
    if (self.uploadIndex == self.choseBigImageLists.count  ){
        return;
    }
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    
    UIImage *obj = self.choseBigImageLists[0];
    NSData *data = UIImageJPEGRepresentation(obj, 0.3);
    
    WS(weakSelf)
    QNUploadOption *option = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
            NSString *message = [NSString stringWithFormat:@"正在上传%zd/%zd张 %2.f%%", weakSelf.uploadIndex + 1, weakSelf.choseBigImageLists.count, percent *  100];
                [SVProgressHUD showWithStatus:message];

        NSLog(@"%f", percent);
    }];
    
    [upManager putData:data key:nil token:self.qiNiuToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
        NSString *urlString= [NSString stringWithFormat:@"http://7xrlrd.com1.z0.glb.clouddn.com/%@,",resp[@"key"]];
        [weakSelf.imageLink appendString:urlString];
        
        NSLog(@"%@",weakSelf.imageLink);
        if (weakSelf.uploadIndex + 1 >= weakSelf.choseBigImageLists.count){ // 最后一样
            
            [SVProgressHUD  dismiss];
            if (weakSelf.uploadImageCompleteHandle){
                weakSelf.uploadImageCompleteHandle(weakSelf.imageLink);
            }
        }else{
            weakSelf.uploadIndex ++;
            [weakSelf uploadImagesuccess: nil];
        }
    } option:option];
    
}
@end
